package com.lwb.ztx.news.widget;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lwb.ztx.R;
import com.lwb.ztx.bean.ProfileBean;
import com.lwb.ztx.bean.Profiles;
import com.lwb.ztx.main.widget.MainActivity;
import com.lwb.ztx.utils.ListUtils;
import com.lwb.ztx.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {@link NewsFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NewsFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private TabLayout mTablayout;
    private ViewPager mViewPager;
    private ArrayList<String> newsTypeName = new ArrayList<>();

    private Profiles newsProfiles;

    private View rootView;

    private List<ProfileBean> profileBeanList;

    public NewsFragment() {
        // Required empty public constructor
    }

    public void setNewsProfiles(Profiles newsProfiles) {
        this.newsProfiles = newsProfiles;
    }

    public Profiles getNewsProfiles() {
        return newsProfiles;
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment NewsFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static NewsFragment newInstance(String param1, String param2) {
        NewsFragment fragment = new NewsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.i("newFragment create");
        initMainType();
        if (getArguments() != null) {
            if(newsProfiles == null) {
                newsProfiles = (Profiles) getArguments().getSerializable("param1");
            }
            profileBeanList = new ArrayList<>();
            List<ProfileBean> beans = newsProfiles.getProfileBeanList();
            for(int i=0;i<beans.size();i++) {
                if(newsTypeName.contains(beans.get(i).getName())) {
                    profileBeanList.add(beans.get(i));
                }
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (ListUtils.getSize(profileBeanList) == 0) {
            profileBeanList = MainActivity.profiles.getProfileBeanList();
        }
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_news, container, false);
            initView(rootView);
        }
        ViewGroup parentView = (ViewGroup) rootView.getParent();
        if (parentView != null) {
            parentView.removeView(rootView);
        }
        return rootView;
//        View view = inflater.inflate(R.layout.fragment_news, container, false);
//        initView(view);
//        return view;
    }

    private void initView(View view) {
        mTablayout = (TabLayout) view.findViewById(R.id.tab_layout);
        mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        mViewPager.setOffscreenPageLimit(3);
        setupViewPager(mViewPager, newsProfiles);
        if(null != profileBeanList) {
            for(int i=0;i<profileBeanList.size();i++) {
                mTablayout.addTab(mTablayout.newTab().setText(profileBeanList.get(i).getName()));
            }
        } else {
            for(int i=0;i<newsTypeName.size();i++) {
                mTablayout.addTab(mTablayout.newTab().setText(newsTypeName.get(i)));
            }
        }
        mTablayout.setupWithViewPager(mViewPager);
    }

    private void setupViewPager(ViewPager mViewPager, Profiles profiles) {
        //Fragment中嵌套使用Fragment一定要使用getChildFragmentManager(),否则会有问题
        MyPagerAdapter adapter = new MyPagerAdapter(getChildFragmentManager());
        if(null != profileBeanList) {
            for (int i=0;i<profileBeanList.size();i++)
            adapter.addFragment(new NewsListFragment(NewsListFragment.NEWS_TYPE, profileBeanList.get(i).getApi()), profileBeanList.get(i).getName());
        } else {
            for(int i=0;i<newsTypeName.size();i++) {
                adapter.addFragment(new NewsListFragment(NewsListFragment.NEWS_TYPE, null), newsTypeName.get(i));
            }
        }
        mViewPager.setAdapter(adapter);
    }

    public static class MyPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public void addFragment(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }
    }


    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    private void initMainType() {
        this.newsTypeName.add("头条");
        this.newsTypeName.add("推荐");
        this.newsTypeName.add("娱乐");
        this.newsTypeName.add("财经");
        this.newsTypeName.add("科技");
        this.newsTypeName.add("军事");
        this.newsTypeName.add("NBA");
        this.newsTypeName.add("美女");
        this.newsTypeName.add("体育");
        this.newsTypeName.add("历史");
        this.newsTypeName.add("汽车");
        this.newsTypeName.add("时尚");
        this.newsTypeName.add("房产");
        this.newsTypeName.add("国际");
        this.newsTypeName.add("时政");
        this.newsTypeName.add("FUN来了");
        this.newsTypeName.add("数码");
        this.newsTypeName.add("游戏");
        this.newsTypeName.add("港澳");
        this.newsTypeName.add("文化");
        this.newsTypeName.add("博报");
        this.newsTypeName.add("健康");
        this.newsTypeName.add("读书");
        this.newsTypeName.add("评论");
        this.newsTypeName.add("佛教");
        this.newsTypeName.add("旅游");
        this.newsTypeName.add("台湾");
        this.newsTypeName.add("电影");
        this.newsTypeName.add("专题");
        this.newsTypeName.add("跑步");
        this.newsTypeName.add("酒业");
        this.newsTypeName.add("暖新闻");
        this.newsTypeName.add("萌物");
    }
}
